package api.google.android;

import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.provider.Settings.Secure;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;


public class MainMenu extends Activity {
	
	private final String memberText = "(Member)";
	private Spinner spinner = null;	
	private String securityKey = "";
        
        @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main2);
        final MainMenu activity = this;
 
        
        securityKey = Secure.getString(getApplicationContext().getContentResolver(), Secure.ANDROID_ID);          
        spinner = (Spinner) findViewById(R.id.spinner);
        UpdateSpinner();
        
        Button mapView = (Button) findViewById(R.id.view_map);
        mapView.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                String currentGroupName = spinner.getSelectedItem().toString();
                if(currentGroupName.contains(memberText)){
	                Intent myIntent = new Intent(view.getContext(), GoogleMapActivity.class);
	                myIntent.putExtra("groupName", currentGroupName.replace(memberText, ""));
	                startActivityForResult(myIntent, 0);
                } else {
                	Toast.makeText(
                			view.getContext(), 
                			"You are not member of group named: " + currentGroupName.replace(memberText, ""), 
                			Toast.LENGTH_SHORT).show();
                }
            }
        });
        
        Button createNew = (Button) findViewById(R.id.create);
        createNew.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                Connection connection = new Connection("create");
                PopupTemplate popup = new PopupTemplate(view.getContext(),
                                "Create new Group",
                                "Enter group name here: ",
                                "Create",
                                connection,
                                activity);
                popup.show();
            }
        });        
        
        Button join = (Button) findViewById(R.id.join);
        join.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {             
                String currentGroupName = spinner.getSelectedItem().toString();
                if(currentGroupName.contains(memberText)){
                	Toast.makeText(
                			view.getContext(), 
                			"You are already member of group named: " + currentGroupName.replace(memberText, ""), 
                			Toast.LENGTH_SHORT).show();
                } else {
                	Connection connection = new Connection("join");
	                connection.addPostData("group", currentGroupName.replace(memberText, ""));
	                PopupTemplate popup = new PopupTemplate(view.getContext(),
	                                "Join a group",
	                                "Enter your name: ",
	                                "Join",
	                                connection,
	                                activity);
	                popup.show();        	
                }
            }
        }); 
        
        Button viewMembers = (Button) findViewById(R.id.view_members);
        viewMembers.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                Intent myIntent = new Intent(view.getContext(), ListView.class);
                myIntent.putExtra("groupName", spinner.getSelectedItem().toString().replace(memberText, ""));
                startActivityForResult(myIntent, 0);
            }
        });
    }
        public void UpdateSpinner(){
            Connection connection = new Connection("groupList");
            connection.addPostData("securityKey", securityKey);
            String jsonResponse = connection.postData();
            ParseJSON jsonParser = new ParseJSON();
            List<String> array_spinner = jsonParser.JsonArrayToList(jsonResponse); 	
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, array_spinner);
            spinner.setAdapter(adapter);
        }
}
